<?php

class InfrastructuresController extends AppController {

    var $name = "Infrastructures";

    function index($poll_id) {
        $this->set('usuario', $this->Auth->user());
        $this->Infrastructure->recursive = 0;
        $this->set('infrastructures', $this->Infrastructure->find('all', array('conditions' => array('Infrastructure.poll_id' => $poll_id))));
        $this->data['Infrastructure']['poll_id'] = $poll_id;

        //verifico si esta encuesta ya fue bloqueada, si es asi no se podran
        //imprimir los links para adicionar o modificar infraestructuras.
        App::Import('model', 'Poll');
        $p = new Poll();
        $p->recursive = -1;

        $this->set('visto_bueno', $p->find('first', array('conditions' => array('Poll.id' => $poll_id), 'fields' => array('Poll.visto_bueno'))));
    }

    function add($poll_id) {
        if (!empty($this->data)) {
            if ($this->Infrastructure->saveAll($this->data)) {
                $this->Session->setFlash("Información de infraestructura adicionada");
                $this->redirect(array('controller' => "Infrastructures", 'action' => 'index', $poll_id));
            }
        } else {
            $this->data['Infrastructure']['poll_id'] = $poll_id;
        }
    }

    function edit($id, $poll_id) {
        if (!empty($this->data)) {
            if ($this->Infrastructure->save($this->data)) {
                $this->Session->setFlash("Información de infraestructura adicionada");
                $this->redirect(array('controller' => "Infrastructures", 'action' => 'index', $poll_id));
            }
        } else {
            $this->data = $this->Infrastructure->find("first", array("conditions" => array("Infrastructure.id" => $id)));
        }
    }

    function delete($id, $poll_id) {
        if ($this->Infrastructure->delete($id)) {
            $this->Session->setFlash('Información de infraestructura borrada con exito');
            $this->redirect(array('controller' => 'Infrastructures', 'action' => 'index', $poll_id));
        }
    }

}

?>